package com.gmail.skwong.speedplayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.Set;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.widget.ImageView;

/**
 * Class responsible for keeping track the music files on the device. The main
 * activity asks this class for songs of a certain BPM
 * 
 * @author Stephen
 * 
 */
@SuppressLint("NewApi")
public class SongsManager {

	private ArrayList<HashMap<String, String>> songList;
	private HashMap<Integer, ArrayList<String>> songTempos;
	private TempoCalculator calc;
	private MediaMetadataRetriever metadata;
	private boolean hasMetadataRetriever;

	@SuppressLint("NewApi")
	public SongsManager() {
		songList = (new SongsImporter()).getSongsList();
		songTempos = new HashMap<Integer, ArrayList<String>>();
		calc = new TempoCalculator();

		if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
			hasMetadataRetriever = false;
		} else {
			hasMetadataRetriever = true;
			metadata = new MediaMetadataRetriever();
		}
		/*
		 * try { Class.forName("android.media.MediaMetadataRetriever");
		 * hasMetadataRetriever = true; metadata = new MediaMetadataRetriever();
		 * } catch (ClassNotFoundException cnf) { hasMetadataRetriever = false;
		 * }
		 */
	}

	public void calculateTempos() {
		for (HashMap<String, String> song : songList) {
			int tempo = calc.getTempo(song.get("songPath"));
			if (songTempos.containsKey(tempo)) {
				ArrayList<String> list = songTempos.get(tempo);
				list.add(song.get("songTitle"));
				songTempos.put(tempo, list);
			} else {
				ArrayList<String> list = new ArrayList<String>();
				list.add(song.get("songTitle"));
				songTempos.put(tempo, list);
			}
		}
	}

	public int getMinTempo() {
		Set<Integer> tempos = songTempos.keySet();
		return java.util.Collections.min(tempos);
	}

	public int getMaxTempo() {
		Set<Integer> tempos = songTempos.keySet();
		return java.util.Collections.max(tempos);
	}

	public String getSongForTempo(int tempo) {
		int i = 0;
		while (i < getMaxTempo()) {
			int upTempo = tempo + i;
			int downTempo = tempo - i;
			if (songTempos.containsKey(upTempo)) {
				ArrayList<String> list = songTempos.get(upTempo);
				Random rand = new Random();
				int index = rand.nextInt(list.size());
				return list.get(index);
			}
			if (songTempos.containsKey(downTempo)) {
				ArrayList<String> list = songTempos.get(downTempo);
				Random rand = new Random();
				int index = rand.nextInt(list.size());
				return list.get(index);
			}
			i++;
		}
		return null;
	}

	public String getSongPath(String title) {
		for (HashMap<String, String> song : songList) {
			if (song.get("songTitle").equalsIgnoreCase(title)) {
				return song.get("songPath");
			}
		}
		return null;
	}

	@SuppressLint("NewApi")
	public void setArt(String path, final ImageView view) {
		if (!hasMetadataRetriever) {
			// view.setImageResource(R.drawable.adele);
		} else {
			metadata.setDataSource(path);
			byte[] art = metadata.getEmbeddedPicture();
			if (art == null) {
				view.post(new Runnable() {

					@Override
					public void run() {
						view.setImageResource(R.drawable.adele);
					}

				});
				// view.setImageResource(R.drawable.adele);
			} else {
				final Bitmap bitmap = BitmapFactory.decodeByteArray(art, 0,
						art.length);
				view.post(new Runnable() {

					@Override
					public void run() {
						view.setImageBitmap(bitmap);
					}

				});
				// view.setImageBitmap(bitmap);
			}
		}

	}
}